package light.head.controller.oa;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.ConsumingDAO;
import light.head.dao.oa.ConsumingDetailDAO;
import light.head.dao.oa.ConsumingItemDAO;
import light.head.dao.oa.GoodsDAO;
import light.head.dao.oa.InStockDAO;
import light.head.dao.oa.InStockItemDAO;
import light.head.dao.oa.StockDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.MD5Utils;
import light.head.model.Consuming;
import light.head.model.ConsumingDetail;
import light.head.model.ConsumingItem;
import light.head.model.InStock;
import light.head.model.InStockItem;
import light.head.model.Organ;
import light.head.model.Stock;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 物品库存信息及入库和领用
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_STOCK)
public class StockController extends SupporterController{
	
	private StockDAO sDAO;
	private InStockDAO insDAO;
	private GoodsDAO gDAO;
	private OrganDAO organDAO;
	private InStockItemDAO insiDAO;
	private ConsumingDAO conDAO;
	private ConsumingItemDAO itemDAO;
	private ConsumingDetailDAO detailDAO;
	private UserInfoDAO uDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer cu_organCode = cu_organCode(request);
		List<Organ> organs = organDAO.getSonsList(cu_organCode);
		request.setAttribute("organs", organs);
		return new ModelAndView(Url.STOCK_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.SORT,Parm.DELETED,Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if(sDAO.findByMapAndOcs(organDAO.getSonCodeList(oc),map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.STOCK_LIST);
	}
		
	@RequestMapping(value = Url.IN)
	public ModelAndView in(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			Stock s = sDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.STOCK, s);
		}
		return new ModelAndView(Url.STOCK_INSTOCK);
	}
	
	//入库
	@Transactional
	@RequestMapping(value = Url.INSTOCK)
	public void InStock(InStock ins, HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<InStockItem> list = ins.getItemList();			// 提交上来的数组
		
		ins.setCt(new Date());
		ins.setDeleted(false);
		ins.setCu(cu_code(request));
		ins.setOc(cu_organCode(request));
		ins.setSort(Byte.parseByte(Sys.S_ONE));
		insDAO.add(ins); 	// 入库记录
		// 保存
		for (InStockItem item : list) {
			// 保存入库明细
			item.setSc(ins.getCode());
			item.setDeleted(false);
			insiDAO.add(item);
			// 保存库存
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<Stock> dbList = sDAO.findByGC(item.getGc(), organDAO.getSonCodeList(oc));
			if (null != dbList && dbList.size() > 0) {
				dbList.get(0).setStockcount(dbList.get(0).getStockcount() + item.getCount());
				sDAO.upd(dbList.get(0));
			} else {
				Stock sto = new Stock();
				sto.setGc(item.getGc());
				sto.setOc(ins.getOc());
				sto.setDeleted(false);
				sto.setStockcount(item.getCount());
				sDAO.add(sto);
			}
		}
		print(response, Msg.SUCCESS);
		
	}

	/**
	 * 查询商品
	 */
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.INDEX, request.getParameter(Parm.INDEX));
		System.out.println(request.getParameter(Parm.INDEX));
		return new ModelAndView(Url.INSTOCK_GOODS_INIT);
	}

	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME,Parm.ENAME,Parm.OC,Parm.GTN);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		gDAO.findByMapAndOcs(organDAO.getSonCodeList(oc), map, pageBean);
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		return new ModelAndView(Url.INSTOCK_GOODS_LIST);
	}
	
	
	
	
	//领用
	@RequestMapping(value = Url.CONSUMING)
	public ModelAndView consuming(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			Stock s = sDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.STOCK, s);
		}
		return new ModelAndView(Url.STOCK_CONSUMING);
	}
	
	
	@Transactional // 事务
	@RequestMapping(value = Url.OUTSTOCK)
	public void outStock(Consuming con, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		
		List<ConsumingItem> list = con.getItemList();			// 提交上来的数组
		con.setCt(new Date());
		con.setDeleted(false);
		con.setConfirm(true);
		con.setCu(cu_code(request));
		con.setOc(cu_organCode(request));
		result = conDAO.add(con); 	// 入库记录
		
		for (ConsumingItem item : list) {
			// 保存领用明细
			
			item.setCoc(con.getCode());
			item.setDeleted(false);
			result = itemDAO.add(item);
		
			// 保存库存
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<Stock> dbList = sDAO.findByGC(item.getGc(), organDAO.getSonCodeList(oc));
			if(dbList.get(0).getStockcount() <= 0){
				print(response,Msg.EXISTS);
				return;
			}else if(null != dbList && dbList.size() > 0) {
				dbList.get(0).setStockcount(dbList.get(0).getStockcount() - item.getCount());
				result = sDAO.upd(dbList.get(0));
			}
			if(dbList.get(0).getSort().intValue() == 2){
				//保存领用详情
				for(ConsumingDetail detail : item.getDetailList()){
					if(detail.getTag() != null && detail.getTag() != ""){
						detail.setCic(item.getCode());
						detail.setDeleted(false);
						result = detailDAO.add(detail);
					}
				}
			}
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(value = "verify")
	public void tag(HttpServletRequest request, HttpServletResponse response){
		String tag = request.getParameter("tag");
		List<ConsumingDetail> detailList = detailDAO.findByTag(tag);
		if (detailList != null && detailList.size() > 0) {
			print(response, Msg.ERROR);
		} else {
			print(response, Msg.SUCCESS);
		}
	}
		
	
	/**
	 * 查询库存的商品
	 */
	@RequestMapping(Url.AIDCON_INIT)
	public ModelAndView aidGoodsInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.INDEX, request.getParameter(Parm.INDEX));
		System.out.println(request.getParameter(Parm.INDEX));
		return new ModelAndView(Url.STOCK_GOODS_INIT);
	}
	
	@RequestMapping(Url.AIDCON_LIST)
	public ModelAndView aidGoodsList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME,Parm.ENAME,Parm.OC,Parm.SORT,Parm.GTC);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		sDAO.findByMapAndOcs(organDAO.getSonCodeList(oc), map, pageBean);
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		return new ModelAndView(Url.STOCK_GOODS_LIST);
	}
	
	/**
	 * 查询用户
	 */
	@RequestMapping(Url.AIDUSER_INIT)
	public ModelAndView aidUserInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.STOCK_USER_INIT);
	}

	@RequestMapping(Url.AIDUSER_LIST)
	public ModelAndView aidUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.REAL_NAME,Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		uDAO.findByMapAndOc(organDAO.getSonCodeList(oc),map,pageBean);
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		return new ModelAndView(Url.STOCK_USER_LIST);
	}
	
	//验证密码
	@RequestMapping(value = Url.AFFIRM)
	public void affirm(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cc= request.getParameter(Parm.CC);
		String pwd = request.getParameter(Parm.PWD);

		if (cc == "" || pwd == "") {
			print(response,Msg.PARAMETER_ERROR);
			return;
		}
		UserInfo user = uDAO.getAffirm(Integer.parseInt(cc), pwd);
		if (user == null) {
			print(response, Msg.ERROR);
			return;
		}
		if (!user.getPwd().equals(MD5Utils.encryptTo32(pwd))) {
			print(response, Msg.ERROR);
		} else {
			print(response, Msg.SUCCESS);
		}
	}


	
	@Autowired
	public void setsDAO(StockDAO sDAO) {
		this.sDAO = sDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setInsDAO(InStockDAO insDAO) {
		this.insDAO = insDAO;
	}
	@Autowired
	public void setgDAO(GoodsDAO gDAO) {
		this.gDAO = gDAO;
	}
	@Autowired
	public void setInsiDAO(InStockItemDAO insiDAO) {
		this.insiDAO = insiDAO;
	}
	@Autowired
	public void setConDAO(ConsumingDAO conDAO) {
		this.conDAO = conDAO;
	}

	@Autowired
	public void setuDAO(UserInfoDAO uDAO) {
		this.uDAO = uDAO;
	}
	@Autowired
	public void setItemDAO(ConsumingItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	@Autowired
	public void setDetailDAO(ConsumingDetailDAO detailDAO) {
		this.detailDAO = detailDAO;
	}
	
}
